﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using final_darskade.Models;
using final_darskade.Models.klassModel;
namespace final_darskade.Controllers
{
    public class EditPrivilegeController : Controller
    {
        darskadeEntities de = new darskadeEntities();

        //
        // GET: /EditPrivilege/

        public ActionResult Index()
        {

            // List<EditPrivilege> editpr = new List<EditPrivilege>();
            //join klass with course
            var klist = (from klass in de.klasses
                         join course in de.courses
                         on klass.course_id equals course.course_id
                         // where   true // klass.id == klass.id
                         select new { klass.id, klass.course_id, klass.prof_id, klass.term, course.name });
            //join klist with user
            var temp = (from k in klist
                        join user in de.users
                        on k.prof_id equals user.user_id
                        //where k.prof_id == k.prof_id
                        //select new EditPrivilege( k.id, k.name, user.first_name, user.last_name , k.term, k.prof_id));
                        select new { k, user.first_name, user.last_name }).ToList();

            var editpr = new List<EditPrivilege>();
            int i = 0;
            foreach (var p in temp)
            {
                EditPrivilege item = new EditPrivilege();
                item.klass_id = p.k.id;
                item.klass_name = p.k.name;
                item.proff_name = p.first_name;
                item.proff_lastname = p.last_name;
                item.term = p.k.term;
                item.proff_id = p.k.prof_id;
                editpr.Add(item);
                //editpr.Insert(i, item);
                //i++;
            }


            return View("HomeEditPrivilege", editpr.ToList());
        }
        //[HttpGet]
        public ActionResult proffesor_privilege(EditPrivilege model)
        {


            // klassmember join with privilege 
            var query = (from x in de.klass_members
                         //where x.klass_id == klassid & x.member_id == proff_id
                         join p in de.privileges
                         on x.privilege_id equals p.privilege_id
                         where x.klass_id == model.klass_id & x.member_id == model.proff_id
                         select new { p.privilege_id, p.privilege_name });


            // List<klass2> klist = new List<klass2>();
            // klass2 item;
            EditProffesorPrivilege editp = new EditProffesorPrivilege();
            editp.prof_id = model.proff_id;
            editp.klass_id = model.klass_id;
            editp.name = model.proff_name;
            editp.last_name = model.proff_lastname;
            editp.klass_name = model.klass_name;
            foreach (var j in query)
            {

                switch (j.privilege_name)
                {
                    case "membership_confirm": editp.membership_confirm = true; break;
                    case "membership_to_klass": editp.membership_to_klass = true; break;
                    case "membership_remove": editp.membership_remove = true; break;
                    case "TA_privilege": editp.TA_privilege = true; break;
                }

            }

            return View("proffesor_Privilege", editp);
        }

        [HttpPost]
        public ActionResult edit([Bind(Exclude = "isChecked_membership_confirm,isChecked_membership_remove,isChecked_membership_to_klass,isChecked_TA_privilege,editp_klass.term")] EditProffesorPrivilege model)
        {
            EditProffesorPrivilege emodel = new EditProffesorPrivilege();
            emodel = model;
            //var query1 = (from klass_members in de.klass_members
            //              where klass_members.member_id == emodel.prof_id &&
            //              klass_members.klass_id == emodel.klass_id
            //              select klass_members);
            var tempquery = (from d in de.klass_members
                             join privilege in de.privileges
                             on d.privilege_id equals privilege.privilege_id
                             where d.klass_id == emodel.klass_id && d.member_id == emodel.prof_id //d.membership_id == j.membership_id
                             select new { d, privilege.privilege_name });
            //List<klass2> kmlist = new List<klass2>();
            //foreach (var m in tempquery) {
            //    klass2 newitem = new klass2(m.d, m.privilege_name);
            //    kmlist.Add(newitem);

            //}


            foreach (var j in tempquery)
            {

                #region switch case
                switch (j.privilege_name)
                {
                    case "membership_confirm": if (emodel.membership_confirm.Equals(true)) { emodel.isChecked_membership_confirm = true; break; }
                        else
                        {
                            // klass_members del = (from delete in de.klass_members where delete.membership_id == j.d.membership_id select delete);
                            klass_members del = de.klass_members.Single(u => u.membership_id == j.d.membership_id);
                            de.klass_members.DeleteObject(del);
                            de.SaveChanges();
                            emodel.isChecked_membership_confirm = true;
                            break;
                        }

                    case "membership_to_klass": if (emodel.membership_to_klass.Equals(true)) { emodel.isChecked_membership_to_klass = true; break; }
                        else
                        {

                            klass_members del = de.klass_members.Single(u => u.membership_id == j.d.membership_id);
                            de.klass_members.DeleteObject(del);
                            de.SaveChanges();
                            emodel.isChecked_membership_remove = true;
                            break;
                        }
                    case "membership_remove": if (emodel.membership_remove.Equals(true)) { emodel.isChecked_membership_remove = true; break; }
                        else
                        {
                            klass_members del = de.klass_members.Single(u => u.membership_id == j.d.membership_id);
                            de.klass_members.DeleteObject(del);
                            de.SaveChanges();
                            break;
                        }
                    case "TA_privilege": if (emodel.TA_privilege.Equals(true)) { emodel.isChecked_TA_privilege = true; break; }
                        else
                        {
                            klass_members del = de.klass_members.Single(u => u.membership_id == j.d.membership_id);
                            de.klass_members.DeleteObject(del);
                            de.SaveChanges();
                            emodel.isChecked_TA_privilege = true;
                            break;
                        }

                }
                #endregion
                if (emodel.isChecked_membership_confirm == false)
                {
                    klass_members newitem = new klass_members();
                    newitem.membership_id = 23;
                    newitem.member_id = emodel.prof_id;
                    newitem.member_id = (from k in de.privileges where k.privilege_name == "membership_confirm" select k.privilege_id).Single();
                    newitem.klass_id = emodel.klass_id;
                    de.AddToklass_members(newitem);
                    de.SaveChanges();
                    emodel.isChecked_membership_confirm = true;
                }
                else if (emodel.isChecked_membership_remove == false)
                {
                    klass_members newitem = new klass_members();
                    newitem.membership_id = 23;
                    newitem.member_id = emodel.prof_id;
                    newitem.member_id = (from k in de.privileges where k.privilege_name == "membership_remove" select k.privilege_id).Single();
                    newitem.klass_id = emodel.klass_id;
                    de.AddToklass_members(newitem);
                    de.SaveChanges();
                    emodel.isChecked_membership_remove = true;
                }
                else if (emodel.isChecked_membership_to_klass == false)
                {
                    klass_members newitem = new klass_members();
                    newitem.membership_id = 23;
                    newitem.member_id = emodel.prof_id;
                    newitem.member_id = (from k in de.privileges where k.privilege_name == "membership_to_klass" select k.privilege_id).Single();
                    newitem.klass_id = emodel.klass_id;
                    de.AddToklass_members(newitem);
                    de.SaveChanges();
                    emodel.isChecked_membership_to_klass = true;
                }
                else //if (emodel.isChecked_TA_privilege == false)
                {
                    klass_members newitem = new klass_members();
                    newitem.membership_id = 23;
                    newitem.member_id = emodel.prof_id;
                    newitem.member_id = (from k in de.privileges where k.privilege_name == "TA_privilege" select k.privilege_id).Single();
                    newitem.klass_id = emodel.klass_id;
                    de.AddToklass_members(newitem);
                    de.SaveChanges();
                    emodel.isChecked_TA_privilege = true;
                }

                //if one or more privilege not exist in table
                //de.ApplyCurrentValues(de.klass_members.EntitySet.Name, klas);

                //de.SaveChanges();

                // }



            }




            return RedirectToAction("Index", "EditPrivilege");

        }

        public ActionResult members_privilege(EditPrivilege ep)
        {
            var query = (from klass in de.klass_members
                         join user in de.users
                             on klass.member_id equals user.user_id
                         where klass.klass_id == ep.klass_id && klass.member_id != ep.proff_id
                         select new { klass.member_id, user.first_name, user.last_name, klass.privilege_id });
            var temp1 = (from qu in query
                         join privilege in de.privileges
                         on qu.privilege_id equals privilege.privilege_id
                         select new { qu, privilege.privilege_name });



            List<klassPrivilege> pklass = new List<klassPrivilege>();

            foreach (var q in temp1)
            {
                klassPrivilege item = new klassPrivilege();
                item.klassName = ep.klass_name;
                item.trm = ep.term;
                item.studentName = q.qu.first_name;
                item.studentLastName = q.qu.last_name;
                switch (q.privilege_name)
                {
                    case "upload_file":
                        item.uploadFilePr = true;
                        break;
                    default: item.uploadFilePr = false;
                        break;
                }//switch
                pklass.Add(item);
            }

            return View("members_privilege", pklass.ToList());
        }

        public ActionResult saveEdit()
        {


            return RedirectToAction("Index", "EditPrivilege");
        }


    }
}
